import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast,Dialog } 	from 'vant';
Vue.use(Toast,Dialog);

export default {
	name: 'editNickName',
	data(){
		return {
			nickName:'',
			btnLoading:false,
			wait:false,
		}
	},
	methods:{
		clicRight(){
			if(this.wait){
				let dialogDebug = window.localStorage.getItem('dialogDebug');
				if(dialogDebug && dialogDebug==='1'){
					//清除悬浮窗
					window.localStorage.removeItem('dialogDebug')
				}else{
					//设定悬浮窗
					window.localStorage.setItem('dialogDebug','1')
				}
			}
		},
		clickLeft(){
			this.wait = true;
			setTimeout(()=>{
				this.wait = false;
			},1000)
		},
		confirm(){
			//确认提交
			//检测不能输入表情
			if(util.isEmojiCharacter(this.nickName)){
				Toast(this.locale==='zh'?'昵称暂不支持表情':'Special characters are not supported')
				return
			}
			if(this.nickName && this.nickName.length > 0 && this.nickName.length <= 20){
				this.btnLoading = true;
				util.fetch(`${util.ajaxUrl}/user/update`,{
					nickname:this.nickName
				}).then(res => {
					if(res.retCode === 0 && res.retData){
						Toast(this.$t('mySetting-key6'));
						//通知native
						this.updateNative();
						setTimeout(() => {
							this.goBack();
							this.btnLoading = false;
						},1000)
					}else{
						this.btnLoading = false;
						Toast(res.retMsg || this.$t('noError'))
					}
				}).catch(error => {
					this.btnLoading = false;
					console.log(error)
				})
			}
		},
		updateNative(){
			util.callHandler('updateUserNickName',{
				nickName:this.nickName
			},res => {
				console.log(res)
			})
		},
		init(){
			
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		if(util.getStorage('realNameStatus')) this.realNameStatus = util.getStorage('realNameStatus');
		util.waitSDKReady().then(token => {
			this.token = token;
			this.init();
		}).catch((e) => {
			Toast(this.$t('getTokenFail'))
		})
		setTimeout(() => {
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				if(this.token) this.init();
				util.callHandler('setStatusBarStyle',{
					style:'dark'
				})
			})
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		},300)
    }
}